<?php

namespace App\Http\Controllers\Api\v1\Pay;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Service\Pay\ExbabaPayService;
use App\Service\Pay\BetcatPayService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Exception;

class BetcatPayController extends Controller
{
    //充值回调
    public function rechargeNotify(Request $request): string
    {
        try {
            $data = $request->all();
            info('BetcatPay收回调参数:'.json_encode($data));
            $order = Order::query()->where('no', '=', $data['orderNo'])->first();
            if (!$order || $order->closed == 1) {
                return 'order error';
            }
            $service = new BetcatPayService($order);
            if (!$service->checkSign($data, 'pay')) {
                return 'sign error';
            }
            /*
            if($order->amount * 100 != $data['orderAmt']){  //金额不一致 直接返回
                return 'SUCCESS';
            }
            */
            $paid_at = Carbon::now()->timestamp;

            if(isset($arr['orderStatus']) && ($arr['orderStatus'] == 3 || $arr['orderStatus'] == 2) ){
                $state = Order::FAILED;
            }else{
                $state = Order::FAILED;
            }
            return (new Order())->rechargeComplete($state, $data['orderNo'], '', $paid_at, json_encode($data, JSON_UNESCAPED_UNICODE)) ? 'success' : 'error';
        } catch (Exception $exception) {
            info('message:' . $exception->getMessage() . ',file:' . $exception->getFile() . ',line:' . $exception->getLine());
            return 'error';
        }
    }

    //提现回调
    public function withdrawalNotify(Request $request): string
    {
        try {
            $arr = $request->all();
            info($arr);
            //$result = json_decode($arr['data'], true);
            $order = Order::query()->where('no', '=', $arr['merOrderNo'])->first();
            if (!$order) {
                return 'order error';
            }
            $service = new BetcatPayService($order);
            if (!$service->checkSign($arr, 'withdrawal')) {
                return 'sign error';
            }

            if(isset($arr['orderStatus']) && ($arr['orderStatus'] == 3 || $arr['orderStatus'] == 2) ){
                $state = Order::SUCCESS;
                $res = (new Order())->withdrawComplete([
                    'state' => $state,
                    'orderId' => $order->no,
                    'paidAt' => Carbon::now()->timestamp
                ], $arr);
                return $res ? 'SUCCESS' : 'error';
            }else if(isset($arr['orderStatus']) && ($arr['orderStatus'] == -1 || $arr['orderStatus'] == -2)){
                $state = Order::FAILED;
                $res = (new Order())->withdrawComplete([
                    'state' => $state,
                    'orderId' => $order->no,
                    'paidAt' => Carbon::now()->timestamp
                ], $arr);
                return $res ? 'success' : 'error';
            }
            return 'error';
        } catch (Exception $exception) {
            return 'error';
        }
    }
}
